const ruleToDeal = {
  ruleCheckChange: function (item, compileRuleIds, attribute) {
    if (item.checked) {
      compileRuleIds.push(item[attribute])
    } else {
      const index = compileRuleIds.indexOf(item[attribute])
      compileRuleIds.splice(index, 1)
    }
  },
  // 规则生成
  compileRule: function ({
    type,
    compileRuleTree,
    listRules,
    compileRuleIds,
    maxLevel,
    attribute
  }) {
    if (type === 'clear') {
      const allNodes = []
      this.getAllNodes(compileRuleTree, allNodes)
      listRules = [...allNodes, ...listRules]
      this.resetCheck(listRules)
      compileRuleTree = []
      return [listRules, compileRuleTree, maxLevel]
    }
    if (compileRuleIds.length <= 1) return []
    // 如果选择已有关联规则
    const compileRuleListTemp = this.getTempRules(
      compileRuleIds,
      compileRuleTree,
      listRules,
      attribute
    );
    [compileRuleTree, maxLevel] = this.filtersRule(
      compileRuleIds,
      compileRuleTree,
      true,
      maxLevel,
      attribute
    )
    const id = new Date().getTime()
    compileRuleTree.push({
      logicType: type,
      rule_id: id,
      id: id,
      children: JSON.parse(JSON.stringify(compileRuleListTemp)),
      level: maxLevel,
      type: 'conditions'
    })
    ;[listRules, maxLevel] = this.filtersRule(
      compileRuleIds,
      listRules,
      true,
      maxLevel,
      attribute
    )
    compileRuleIds = []
    maxLevel = 1
    // 根据ruleid排序
    compileRuleTree = this.sortTree(compileRuleTree)
    return [listRules, compileRuleTree, maxLevel]
  },
  getAllNodes: function (list, rules) {
    for (const item of list) {
      if (item.children) {
        this.getAllNodes(item.children, rules)
      } else {
        rules.push(item)
      }
    }
  },
  resetCheck: function (list) {
    list.map((item) => {
      item.checked = false
      return item
    })
  },
  getTempRules: function (
    ids,
    compileRuleTree,
    listRules,
    attribute
  ) {
    const compileRuleListTemp = []
    for (const item of ids) {
      const hasTree = compileRuleTree.filter((itemT) => {
        return itemT[attribute] === item
      })
      if (hasTree.length == 1) {
        compileRuleListTemp.push(hasTree[0])
      } else {
        const hasList = listRules.filter((itemL) => {
          return itemL[attribute] === item
        })
        if (hasList.length === 1) {
          compileRuleListTemp.push(hasList[0])
        }
      }
    }
    return compileRuleListTemp
  },
  filtersRule: function (
    ids,
    rules,
    noScissors,
    maxLevel,
    attribute
  ) {
    let listRules = JSON.parse(JSON.stringify(rules))
    ids.map((item) => {
      listRules = listRules.filter((it) => {
        if (it.level && item == it[attribute] && noScissors) maxLevel = 2
        return it[attribute] != item
      })
      return item
    })
    return [listRules, maxLevel]
  },
  // 删除
  deleteRuleHander: function (
    data,
    compileRuleTree,
    listRules,
    _,
    cb
  ) {
    // { parent: 当前节点的父节点 , allList: 当前元素的父元素的所有子元素, index}
    data.allList.splice(data.index, 1) // 删掉当前元素
    // 临界值
    if (data.allList.length == 1) {
      let isHigest = false
      let searchIndex = -1
      compileRuleTree.forEach((element, index) => {
        if (!!data.parent && element.rule_id === data.parent.rule_id) {
          isHigest = true
          searchIndex = index
        }
      })
      if (!data.allList[0].logicType && !!data.parent) {
        if (isHigest) {
          // 剩下的放到散列表里
          compileRuleTree.splice(searchIndex, 1)
          listRules.push(data.allList[0])
          this.resetCheck(listRules)
        } else {
          // 提升一层
          Object.assign(data.parent, data.allList[0])
          cb(data)
          _.$set(data.parent, 'checked', false)
          delete data.parent.logicType
          delete data.parent.children
          delete data.parent.level
        }
      } else {
        if (isHigest) {
          compileRuleTree.splice(searchIndex, 1)
          compileRuleTree.push(data.allList[0])
          this.resetCheck(compileRuleTree)
        }
      }
      this.resetLevel(compileRuleTree, listRules)
    }
  },
  resetLevel: function (compileRuleTree, listRules) {
    for (const item of compileRuleTree) {
      if (item.children && item.children.length != 1) {
        item.level = ruleToDeal.hasSon(item.children) ? 2 : 1
      } else {
        listRules.unshift(item)
        let spliceIndex = -1
        compileRuleTree.forEach((element, index) => {
          if (element.rule_id === item.rule_id) {
            spliceIndex = index
          }
        })
        compileRuleTree.splice(spliceIndex, 1)
      }
    }
  },
  hasSon: function (list) {
    let flag = false
    for (const item of list) {
      if (item.children) {
        item.level = 1
        flag = true
      } else {
        delete item.level
      }
    }
    return flag
  },
  // 移除规则关系
  scissorsHandle: function (
    scissorsNode,
    compileRuleTree,
    listRules,
    maxLevel = 2
  ) {
    if (!scissorsNode.parent && scissorsNode.scissors.logicType) {
      const childs = scissorsNode.scissors.children
      childs.forEach((element, index) => {
        element.checked = false
        if (element.logicType) {
          element.level = 1
          compileRuleTree.push(element)
        } else {
          listRules.push(element)
        }
      })
      let scissorsNodeIndex = -1
      compileRuleTree.forEach((item, index) => {
        if (item.rule_id === scissorsNode.scissors.rule_id) {
          scissorsNodeIndex = index
        }
      })
      if (scissorsNodeIndex !== -1) {
        compileRuleTree.splice(scissorsNodeIndex, 1)
      }
      // console.log('一级and')
    } else if (scissorsNode.parent && scissorsNode.scissors.logicType) {
      const parentChilds = scissorsNode.parent.children
      const allNodes = []
      let scissorsNodeIndex = -1
      parentChilds.forEach((item) => {
        if (item.children) {
          item.children.forEach((cItem) => {
            if (!cItem.logicType) allNodes.push(cItem)
          })
        } else {
          allNodes.push(item)
        }
      })
      this.resetCheck(allNodes)
      listRules.push(...allNodes)
      compileRuleTree.forEach((item, index) => {
        if (item.rule_id === scissorsNode.parent.rule_id) {
          scissorsNodeIndex = index
        }
      })
      if (scissorsNodeIndex !== -1) {
        compileRuleTree.splice(scissorsNodeIndex, 1)
      }
      // console.log('二级and')
    }
  },
  sortTree (tree) {
    tree.forEach((node) => {
      if (node.children && node.children.length > 0) {
        node.children = this.sortTree(node.children)
      }
    })
    return tree.sort((nodeA, nodeB) => {
      const sA = nodeA?.rule_id || 999999999
      const sB = nodeB?.rule_id || 999999999
      return sA - sB
    })
  }
}
export default ruleToDeal
